package org.wang.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.wang.gift.clients.LoginUserClient;
import org.wang.gift.clients.SmsClient;
import org.wang.gift.domain.LoginUser;
import org.wang.gift.domain.User;
import org.wang.gift.dto.RegisterDto;
import org.wang.gift.dto.SmsCodeDto;
import org.wang.gift.exception.asserts.Assert;
import org.wang.gift.exception.code.StatusCode;
import org.wang.gift.mapper.UserMapper;
import org.wang.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wang.gift.util.AjaxResult;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author wangsang
 * @since 2022-10-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SmsClient smsClient;
    @Autowired
    private LoginUserClient loginUserClient;
    @Override
    public AjaxResult smsCodeSend(String mobile) {
        //判断手机号是否为空
        Assert.assertNotEmpty(mobile, StatusCode.CODE_400);
        //数据库查询是否存在该用户
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", mobile));
        //判断数据库查询数据是否为空，为空代表没有该用户
        Assert.assertListIsEmpty(users,StatusCode.CODE_400_TENANT_ADMIN_EXSIT);
        //用户为空继续注册，发送短信验证码
        return smsClient.sendSmsCodeRegister(mobile);
    }

    @Override
    @GlobalTransactional
    public AjaxResult registerByMobile(RegisterDto dto) {
        String mobile = dto.getMobile();
        String password = dto.getPassword();
        String smsCode = dto.getSmsCode();
        //数据库查询是否存在该用户
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", mobile));
        //判断数据库查询数据是否为空，为空代表没有该用户
        Assert.assertListIsEmpty(users,StatusCode.CODE_400_TENANT_ADMIN_EXSIT);
        //验证码校验
        SmsCodeDto smsCodeDto = new SmsCodeDto();
        smsCodeDto.setMobile(mobile);
        smsCodeDto.setCode(smsCode);
        AjaxResult ajaxResult = smsClient.validationSmsCode(smsCodeDto);
        if (!ajaxResult.isSuccess()){
            return ajaxResult;
        }
        //注册
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setPassword(password);
        loginUser.setTel(mobile);
        loginUser.setType(1);
        AjaxResult result = loginUserClient.addOrUpdate(loginUser);
        if (!result.isSuccess())
            return result;
        Long loginUserId = Long.valueOf(result.getResultObj().toString());
        //user
        User user = new User();
        user.setCreateTime(new Date());
        user.setPhone(mobile);
        user.setPassword(password);
        user.setSalt(null);
        user.setBitState(1L);
        user.setSecLevel(1);
        user.setLoginId(loginUserId);
        user.setUsername(loginUser.getUsername());
        userMapper.insert(user);
        return AjaxResult.me();
    }
}
